<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8"/>
    <title></title>
    {include file='header'}
    <style>
        .layui-form-select dl {
            max-height: 200px;
        }
    </style>
</head>

<body>
    <div class="x-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>分类名称
                </label>
                    <div class="layui-input-inline">
                      <input type="text" id="name" name="name" value="{if !empty($data)}{$data.name}{/if}" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
            </div>
            <div class="layui-form-item">
                    <label class="layui-form-label label-required">上传图片</label>
                    <div  class="layui-input-block">
                    <button type="button" class="layui-btn" id="images">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <div class="demo-images">{if !empty($data)}<img src="{$data.icon}" style="height:80px"/>{/if}</div>
                    <input name="icon" type="hidden" value="{if !empty($data)}{$data.icon}{/if}">
                    </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>分类</label>
                <div class="layui-input-inline">
                    <select name="parent_id" lay-filter="parent_id">
                        <option value="{$parent_id}">顶级分类</option>
                        {foreach $category as $v}
                        <option value="{$v.id}"{if $v.id==$pid} selected{/if}>{$v.spl}{$v.name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>排序</label>
                <div class="layui-input-inline">
                    <input type="text" id="sort" name="sort" value="{if !empty($data)}{$data.sort}{else}0{/if}" autocomplete="off" class="layui-input">
                </div>
            </div>            
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red"></span>状态</label>
                <div class="layui-input-inline">
                    <input type="checkbox" name="status" value="1"  lay-text="开启|停用"  {if !empty($data)}{if $data.status==1}checked=""{/if}{else}checked=""{/if} lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <input type="hidden" id="id" name="id" value="{if !empty($data)}{$data.id}{else}0{/if}" />
                <button type="button" class="layui-btn" lay-filter="save" lay-submit="">保存</button>
            </div>
        </form>
    </div>

    <script>
    layui.use('upload', function(){
        var upload = layui.upload;
        var uploadInst = upload.render({
        elem: '#images'    
        ,url: "{:url('wadmin/upload/index')}"
        ,data: { type: "banner" }//额外传输的参数
        // ,exts: 'jpg|png|gif|' //可传输文件的后缀
        // ,size: 100 //传输大小100k
        // ,headers:{token:'XXXXXXXX'} //额外添加的请求头
        // ,multiple: false //多文件上传
        ,done: function(res){
          console.log(res)
          if(res.code==1){
            $(".demo-images").html('<img src="'+res.data.url+'" height=80>');
            $("input[name='icon']").val(res.data.url);
          }
          else
          {
              layer.msg(res.msg)
          }
        }
        ,error: function(){
          layer.msg('上传失败');
        }
        });
    });
        layui.use(['form', 'layer'], function () {
            //监听提交
            var form = layui.form;
            form.on('submit(save)', function (data) {
                //异步Ajax       
                $.ajax({
                    type: "POST",
                    url: "{:url('save')}",
                    data: data.field,
                    datatype: "json",
                    success: function (res, textStatus) {        
                        console.log(res)                
                        if (res.code == 1) {
                            layer.msg(res.msg, { icon: 1,time:1000 },function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.window.location.reload();                                
                            });
                        }
                        else {
                            layer.msg(res.msg, { icon: 5 });//0感叹号  1打勾  2打叉  3问号  4锁头  5难过  6笑脸
                            return false;
                        }
                    },
                    error: function (e) {
                        layer.alert('系统出现错误,请检查系统！', { icon: 2 });
                        return false;
                    }
                });
                return false;
            });
        });

    </script>

</body>

</html>
